import openpyxl
from qrcode import QRCode
import io
import os
import qrcode
from openpyxl.drawing.image import Image

# 加载Excel文件
wb = openpyxl.load_workbook('data.xlsx')
sheet = wb.active
current_row_number = 2

# 遍历每一行
for row in sheet.iter_rows(min_row=2):  # 假设第一行是标题行，从第二行开始
    name = row[0].value  # 假设名字在第一列
    if name:
        # 生成二维码
        qr = QRCode(
            version=1,
            error_correction=qrcode.constants.ERROR_CORRECT_L,
            box_size=5,
            border=1,
        )
        qr.add_data(name)
        qr.make(fit=True)

        img_data = qr.make_image()
        img_data.save('qrcode.png')

        # 将二维码图片插入Excel
        img = Image('qrcode.png')
        img.left, img.top = 1000, 1000
        img.anchor = sheet.cell(current_row_number, 2).coordinate
        sheet.add_image(img)

        # 保存工作簿
        wb.save("data.xlsx")
        current_row_number +=1